<?php

namespace App\Http\Controllers\Customer;

use App\Helpers\Code;
use App\Models\Customer\CustomerDepartment;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class CustomerDepartmentController extends Controller
{

    /**
     * CustomerDepartmentController constructor.
     *
     * @author lql
     */
    public function __construct()
    {
        $this->model = new CustomerDepartment();

        $this->_validators();
    }

    /**
     * 添加客户科室/部门
     * @param Request $request
     * @return array
     *
     * @author lql
     */
    public function addCustomerDepartment(Request $request)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['code', 'created_by', 'updated_by', 'data_scope']));

            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->addCustomerDepartment($data);
            $this->checked($model, [
                'null' => 'customer.departments.title_already_exists',
                Code::INSIDE_ERROR => 'customer.region_province_not_rely',
                Code::INSIDE_ERROR_1 => 'customer.filed_not_exists',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑客户科室/部门
     * @param Request $request
     * @param $id
     * @return array
     *
     * @author lql
     */
    public function editCustomerDepartment(Request $request, $id)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['created_by', 'updated_by', 'data_scope', 'code']));

            $rules = [];
            foreach ($data as $key => $value) {
                switch ($key) {
                    default:
                        $rules[$key] = $this->rules[$key];
                }
            }

            $validator = Validator::make($data, $rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editCustomerDepartment($data, $id);
            $this->checked($model, [
                'null' => 'common.recored_not_found',
                Code::INSIDE_ERROR => 'customer.departments.title_already_exists',
                Code::INSIDE_ERROR_1 => 'customer.region_province_not_rely',
                Code::INSIDE_ERROR_2 => 'customer.filed_not_exists',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取部门/科室详情
     * @param $id
     * @return array
     * todo:根据前端修改
     * @author lql
     */
    public function getCustomerDepartment($id)
    {
        try {
            $model = $this->model->getCustomerDepartment($id);

            $this->checked($model, [
                'null' => 'common.recored_not_found',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validators()
    {
        $this->rules = [
            'customer_id' => 'bail|required|exists:customers,id',
            'title' => 'bail|required|max:100',
            'description' => 'bail|nullable|max:200',
            'phone' => 'bail|nullable|max:20',
            'email' => 'bail|nullable|max:100|email',
            'state' => 'bail|required|in:0,1',
            'field' => 'bail|nullable|max:200',
            'salesman' => 'bail|nullable|exists:users,id',
            'region' => 'bail|required',
            'province' => 'bail|required',
            'rank' => 'bail|nullable|numeric'
        ];

        $this->messages = [
            'customer_id.required' => trans('customer.departments.customer_id_required'),
            'customer_id.exists' => trans('customer.departments.customer_id_exists'),
            'title.required' => trans('customer.title_required'),
            'title.max' => trans('customer.title_max'),
            'description.max' => trans('customer.description_max'),
            'phone.max' => trans('customer.phone_max'),
            'email.email' => trans('customer.email_email'),
            'email.max' => trans('customer.email_max'),
            'state.required' => trans('customer.state_required'),
            'state.in' => trans('customer.state_in'),
            'field.max' => trans('customer.departments.field_max'),
            'salesman.exists' => trans('customer.departments.salesman_exists'),
            'region.required' => trans('customer.region_required'),
            'province.required' => trans('customer.province_required'),
            'rank.numeric' => trans('customer.rank_numeric'),
        ];
    }

}
